package repository

import (
	"gitee.com/fierce_wolf/go-fox-edge-common/commLogger"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Method"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/periodTask"
)

type RepoDownLoadTask struct {
	ModelType string
	ModelName string
	Version   string
	Stage     string
	PathName  string
	Component string
}

func (e *RepoDownLoadTask) TaskType() periodTask.TaskType {
	return periodTask.TaskOnce
}

func (e *RepoDownLoadTask) SchedulePeriod() int64 {
	return 1
}

func (e *RepoDownLoadTask) Execute() {
	// 简单验证
	if Method.HasEmpty(e.ModelType, e.ModelName, e.Version, e.Stage, e.PathName, e.Component) {
		commLogger.Console.Error("参数不能为空: modelType, modelName, version, stage, pathName, component")
		return
	}

	// 删除旧的下载文件
	if "service" == e.ModelType || "decoder" == e.ModelType || "webpack" == e.ModelType || "template" == e.ModelType {
		CloudFileInstallService.DeletePackageFile(e.ModelType, e.ModelName, e.Version, e.Stage, e.Component)
	}

	// 重新下载新的安装文件
	CloudFileInstallService.downloadFile(e.ModelType, e.ModelName, e.Version, e.Stage, e.PathName, e.Component)

	// 下载完成后，验证状态
	CloudFileInstallStatus.scanLocalStatusComponent(e.ModelType, e.ModelName, e.Version, e.Stage, e.Component)
	CloudFileInstallStatus.scanLocalMd5Component(e.ModelType, e.ModelName, e.Version, e.Stage, e.Component)
}
